package com.cat.yygh.hosp.controller.api;

import com.cat.yygh.common.exception.YyghException;
import com.cat.yygh.common.result.R;
import com.cat.yygh.common.util.MD5;
import com.cat.yygh.hosp.service.DepartmentService;
import com.cat.yygh.hosp.service.HospitalService;
import com.cat.yygh.hosp.bean.Result;
import com.cat.yygh.hosp.utils.HttpRequestHelper;
import com.cat.yygh.model.hosp.Hospital;
import com.cat.yygh.vo.hosp.DepartmentVo;
import com.cat.yygh.vo.hosp.HospitalQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(value = "医院管理接口Api")
@RestController
@RequestMapping("/api/hosp")
public class ApiHospitalController {
    /*注入Hospital服务*/
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private DepartmentService departmentService;


    @ApiOperation(value = "医院列表")
    @GetMapping ("/getPageList/{page}/{limit}")
    public R getPageList(@PathVariable Integer page, @PathVariable Integer limit, HospitalQueryVo hospitalQueryVo){
        Page<Hospital> hospitals = hospitalService.getPageList(page,limit,hospitalQueryVo);
        return R.ok().data("pages",hospitals);
    }


    @ApiOperation(value = "上传医院")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        /*将入参转化成键值对*/
        Map<String, Object> resultMap = HttpRequestHelper.switchMap(request.getParameterMap());
        hospitalService.saveHospital(resultMap);
        String SignKey = (String)resultMap.get("sign");
        String HosCode = (String)resultMap.get("hoscode");
        /*从数据库中查询SignKey*/
        String platformSignKey = hospitalService.getSignKeyWithHoscode(HosCode);
        if (SignKey.equals(MD5.encrypt(platformSignKey))){
            /*处理base64 传值时候有问题*/
            String logoData = (String)resultMap.get("logoData");
            /*字符串替换*/
            String result = logoData.replaceAll(" ", "+");
            resultMap.put("logoData",result);
            hospitalService.saveHospital(resultMap);
            return Result.ok();
        }else {
            throw new YyghException(20001,"保存失败");
        }
    }
    @ApiOperation(value = "查询单个医院")
    @PostMapping("/hospital/show")
    public Result<Hospital> getHospitalInfo(HttpServletRequest request){
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)stringObjectMap.get("hoscode");
        String SignKey = (String)stringObjectMap.get("sign");
        String HosCode = (String)stringObjectMap.get("hoscode");
        Hospital hospital = hospitalService.getHoospitalByHoscode(hoscode);
        return Result.ok(hospital);
//        /*从数据库中查询SignKey*/
//        String platformSignKey = hospitalService.getSignKeyWithHoscode(HosCode);
//        if (SignKey.equals(MD5.encrypt(platformSignKey))){
//            Hospital hospital = hospitalService.getHoospitalByHoscode(hoscode);
//            return Result.ok(hospital);
//        }else {
//            throw new YyghException(20001,"查询失败");
//        }
    }
    @ApiOperation(value = "修改医院状态")
    @PutMapping("/updateStatus/{id}/{status}")
    public R updateStatus(@PathVariable String id, @PathVariable Integer status){
        hospitalService.updateStatus(id,status);
        return R.ok();
    }
    @ApiOperation(value = "查询医院详情")
    @GetMapping("/getHospById/{id}")
    public R getHospById(@PathVariable String id){
        Hospital hosp = hospitalService.getHospById(id);
        return R.ok().data("hospital",hosp);
    }
    @ApiOperation(value = "根据医院名称获取议员列表")
    @GetMapping("/getHospByName/{id}")
    public R getHospByName(@PathVariable String id){
        Hospital hosp = hospitalService.getHospById(id);
        return R.ok().data("hospital",hosp);
    }
    @ApiOperation(value = "根据医院名称获取医院列表")
    @GetMapping("/findByHosname/{hosname}")
    public R findByHosname(@PathVariable String hosname){
        List<Hospital> list = hospitalService.findByHosname(hosname);
        return R.ok().data("list",list);
    }
    @ApiOperation(value = "医院预约挂号详情")
    @GetMapping("{hoscode}")
    public R item(
            @ApiParam(name = "hoscode", value = "医院code",required = true)
            @PathVariable String hoscode){
        Map<String,Object> map = hospitalService.item(hoscode);
        return R.ok().data(map);
    }
    @ApiOperation(value = "获取科室列表")
    @GetMapping("department/{hoscode}")
    public R index(
@ApiParam(name = "hoscode", value = "医院code", required = true)
        @PathVariable String hoscode) {
        List<DepartmentVo> list = departmentService.getDepartmentList(hoscode);
        return R.ok().data("list",list);
    }

}
